package com.github.pig.training.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.service.SysUserService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.vo.UserVO;
import com.github.pig.common.web.BaseController;
import com.github.pig.training.entity.TrainingStudent;
import com.github.pig.training.entity.TrainingStudentRegister;
import com.github.pig.training.entity.TrainingStudentUnRegister;
import com.github.pig.training.service.ITrainingStudentRegisterService;
import com.github.pig.training.service.ITrainingStudentService;
import com.github.pig.training.service.ITrainingStudentUnRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/trainingStudentDocuments")
public class TrainingStudentDocumentsController  extends BaseController{
    @Autowired
    private ITrainingStudentService iTrainingStudentService;
    @Autowired
    private ITrainingStudentRegisterService trainingStudentRegisterService;
    @Autowired
    private ITrainingStudentUnRegisterService iTrainingStudentUnRegisterService;
    @Autowired
    private SysUserService userService ;
    @Autowired
    private ITrainingStudentService trainingStudentService;


    /**
     * 查询跟进中的学生
     * @param map
     * @return
     */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String,Object> map){
//        EntityWrapper<SysUser> ew1 = new EntityWrapper<>();
//        ew1.setSqlSelect("user_id","name");
//        List<SysUser> users = userService.selectList(ew1);
//        try {
//            List<TrainingStudent> list = ReadData.setTrainingStudent(users);
//            int i = 0;
//            for(TrainingStudent t :list){
//                i ++ ;
//                trainingStudentService.insert(t);
//            }
//            System.err.println(i);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        EntityWrapper<TrainingStudent> ew = new EntityWrapper<>();
        ew.where("follow_up_status = {0}","1601");
        map.put(CommonConstant.DEL_FLAG,CommonConstant.STATUS_NORMAL);
        return iTrainingStudentService.selectPage(new Query<>(map),ew);
    }

    /**
     * 学员退学
     * @param
     * @return
     */
    @PostMapping("/dropOutOfSchool")
    public R<Boolean> dropOutOfSchool(@RequestBody TrainingStudentUnRegister trainingStudentUnRegister, UserVO userVO) {
        trainingStudentUnRegister.setCreateTime(new Date());
        trainingStudentUnRegister.setUpdateTime(new Date());
        trainingStudentUnRegister.setDelFlag(CommonConstant.STATUS_NORMAL);
        return iTrainingStudentUnRegisterService.dropOutOfSchool(trainingStudentUnRegister,userVO.getUsername());
    }

    /**
     * 学员报名
     * @param trainingStudentRegister
     * @return
     */
    @PostMapping
    public R<Boolean> signUpDocuments(@RequestBody TrainingStudentRegister trainingStudentRegister) {
        trainingStudentRegister.setCreateTime(new Date());
        trainingStudentRegister.setUpdateTime(new Date());
        trainingStudentRegister.setDelFlag(CommonConstant.STATUS_NORMAL);
        return trainingStudentRegisterService.signUpDocuments(trainingStudentRegister);
    }

    @PostMapping("/signUp")
    public R<Boolean> signUp(@RequestBody Map<String,String> params, UserVO user){
        return trainingStudentRegisterService.signUp(params,user);
    }

}
